import http from '@/utils/http'
import type { AxiosRequestConfig } from 'axios'
import { ApiResult } from '@/types/data'

/* 
  封装四个工具函数,发送get,post,put,delete  请求
*/

async function get<T = any>(url: string, config?: AxiosRequestConfig | undefined) {
  const promise = http.get<ApiResult<T>>(url, config)
  const res = await promise
  return res.data
}
async function del<T = any>(url: string, config?: AxiosRequestConfig | undefined) {
  const promise = http.delete<ApiResult<T>>(url, config)
  const res = await promise
  return res.data
}

async function post<T = any>(url: string, data?: any, config?: AxiosRequestConfig | undefined) {
  const promise = http.post<ApiResult<T>>(url, data, config)
  const res = await promise
  return res.data
}

async function put<T = any>(url: string, data?: any, config?: AxiosRequestConfig | undefined) {
  const promise = http.put<ApiResult<T>>(url, data, config)
  const res = await promise
  return res.data
}
async function patch<T = any>(url: string, data?: any, config?: AxiosRequestConfig | undefined) {
  const promise = http.patch<ApiResult<T>>(url, data, config)
  const res = await promise
  return res.data
}
export { get, del, post, put, patch }
